package mo.tags.stack;

import mo.common.TreeNode;

import java.util.Stack;

public class L173 {

    private Stack<TreeNode> nodes = new Stack<>();

    private void addLeft(TreeNode root) {
        TreeNode tmp = root;
        while (tmp != null) {
            nodes.add(tmp);
            tmp = tmp.left;
        }
    }

    public L173(TreeNode root) {
        addLeft(root);
    }

    public int next() {
        TreeNode min = nodes.pop();
        if (min.right != null) {
            addLeft(min.right);
        }
        return min.val;
    }

    public boolean hasNext() {
        return !nodes.isEmpty();
    }

}
